package study.springboot.starter.tacker.rest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Configuration
//Bean存在时加载
@ConditionalOnBean({RestTemplate.class})
public class RestTrackerAutoConfiguration {

    @Autowired
    private List<RestTemplate> restTemplateLt;

    @PostConstruct
    public void addInterceptor() {
        log.info(">>>>>> set interceptor for RestTemplate");
        ClientHttpRequestInterceptor interceptor = new RestHeaderInterceptor();
        List<ClientHttpRequestInterceptor> interceptorLt = Arrays.asList(interceptor);
        for (RestTemplate restTemplate : restTemplateLt) {
            restTemplate.setInterceptors(interceptorLt);
        }
    }
}
